package com.easy.controller;

import com.easy.bean.Cart;
import com.easy.bean.Cart;
import com.easy.bean.User;
import com.easy.service.ICartService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    ICartService cartService;

    @RequestMapping("get")
    @ResponseBody
    public List<Cart> getCartListById(HttpSession session){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        return cartService.cartList(userid);
    }
    
    @RequestMapping("list")
    @ResponseBody
    public LayData getCartList(Page page, String text){
        List<Cart> list=cartService.getCartList(page,text);
        int count=cartService.CartCount(text);
        return new LayData(count,list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int deleteCart(HttpSession session,int proid){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        return cartService.deleteCart(userid,proid);
    }

    @RequestMapping("add")
    @ResponseBody
    public int addCart(HttpSession session,Cart cart){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        cart.setUserid(loginUser.getId());
        return cartService.addCart(cart);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int editCart(HttpSession session,Cart cart){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        cart.setUserid(loginUser.getId());
        return cartService.editCart(cart);
    }

    @RequestMapping("sum")
    @ResponseBody
    public double sumCart(HttpSession session){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        return cartService.totalPrice(userid);
    }

    @RequestMapping("deletechecked")
    @ResponseBody
    public int deletechecked(HttpSession session){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        return cartService.deletechecked(userid);
    }
}
